<?php
require_once "koneksi.php";
require_once "lib.php";

$data_siswa=get_data_siswa();
ini_set('max_execution_time', 7200);
?>
<style>
	.alert {
		padding: 15px;
		margin-bottom: 20px;
		border: 1px solid transparent;
		border-radius: 4px;
		width: 300px;
		margin-left: auto;
		margin-right: auto;
		margin-top: 100px;
		font-family: 'Arial';
		font-size: 14px;
		text-align: center;
	}
	
	.alert-info {
		color: #31708f;
		background-color: #d9edf7;
		border-color: #bce8f1;
	}

</style>
<div class='alert alert-info'>Proses Pengiriman Email</div>
<?php
foreach($data_siswa as $key=>$val){
	$tapel_aktif=get_tapel_aktif();
	$periode_aktif=get_periode_aktif();
	$detail_wali=get_detail_wali_email($val['nis']);
	$detail_kelas=detail_kelas($val['nis'],$tapel_aktif['id_mr_th_ajaran']);
	$email_siswa = $val['email'];

	$tagihan_bulanan=get_komp_bulanan($tapel_aktif['id_mr_th_ajaran'],$val['nis'],$periode_aktif['id']);
	$tagihan_sekali=get_tagihan_sekali($val['id_tapel_masuk'],$val['nis']);
	$tagihan_tahunan=get_tagihan_tahunan($tapel_aktif['id_mr_th_ajaran'],$val['nis']);

	$str_sms="Informasi Tagihan Buat Ortu ".$val['nm_lengkap']." : ";
	$str_bulanan="Tagihan Bulanan ";
	$jml_bulanan=count($tagihan_bulanan);
	$no_bulanan=1;
	$arr_tagihan=array();
	if(!empty($tagihan_bulanan)){
		$str_tagihan_bulanan="Tagihan bulanan dengan rincian ";
		$str_tagihan_bulanan.="<table>";
		foreach($tagihan_bulanan as $val_bulanan){
			$arr_tagihan[]=array('komponen'=>$val_bulanan['nama_komp'],'nominal'=>$val_bulanan['nominal']);
			if($no_bulanan==$jml_bulanan){
				$str_bulanan.=$val_bulanan['nama_komp']." : Rp. ".number_format($val_bulanan['nominal'],0,",",".")." ";
			}else{
				$str_bulanan.=$val_bulanan['nama_komp']." : Rp. ".number_format($val_bulanan['nominal'],0,",",".").", ";
			}
			
			$no_bulanan++;
			//buat tabel
			$str_tagihan_bulanan.="<tr>";
			$str_tagihan_bulanan.="<td width='175'>".$val_bulanan['nama_komp']."</td>";
			$str_tagihan_bulanan.="<td>:</td>";
			$str_tagihan_bulanan.="<td>Rp.</td>";
			$str_tagihan_bulanan.="<td align='right' width='100'>".number_format($val_bulanan['nominal'],0,",",".")."</td>";
			
			$str_tagihan_bulanan.="</tr>";
		}
		$str_tagihan_bulanan.="</table>";
	}else{
		$str_tagihan_bulanan="";
	}
	//echo $str_tagihan_bulanan;
	
	//untuk tahunan
	$str_tahunan="Tagihan Tahunan ";
	$jml_tahunan=count($tagihan_tahunan);
	$no_tahunan=1;
	if(!empty($tagihan_bulanan)){
		$str_tagihan_tahunan="Tagihan tahunan dengan rincian ";
		$str_tagihan_tahunan.="<table>";
		foreach($tagihan_tahunan as $val_tahunan){
			$arr_tagihan[]=array('komponen'=>$val_tahunan['nama_komp'],'nominal'=>$val_tahunan['nominal']);
			if($no_tahunan==$jml_tahunan){
				$str_tahunan.=$val_tahunan['nama_komp']." : Rp. ".number_format($val_tahunan['nominal'],0,",",".")." ";
			}else{
				$str_tahunan.=$val_tahunan['nama_komp']." : Rp. ".number_format($val_tahunan['nominal'],0,",",".").", ";
			}
			
			$no_tahunan++;
			//buat tabel
			$str_tagihan_tahunan.="<tr>";
			$str_tagihan_tahunan.="<td width='175'>".$val_tahunan['nama_komp']."</td>";
			$str_tagihan_tahunan.="<td>:</td>";
			$str_tagihan_tahunan.="<td>Rp.</td>";
			$str_tagihan_tahunan.="<td align='right' width='100'>".number_format($val_tahunan['nominal'],0,",",".")."</td>";
			
			$str_tagihan_tahunan.="</tr>";
		}
		$str_tagihan_tahunan.="</table>";
	}else{
		$str_tagihan_tahunan="";
	}
	//akhir tahunan
	//echo $str_tagihan_tahunan;
	
	$str_sekali="Tagihan Sekali : ";
	$jml_sekali=count($tagihan_sekali);
	$no_sekali=1;
	if(!empty($tagihan_bulanan)){
		$str_tagihan_sekali="Tagihan Lain-lain dengan rincian ";
		$str_tagihan_sekali.="<table>";
		foreach($tagihan_sekali as $val_sekali){
			$bayar=get_bayar_sekali($val['nis'],$val_sekali['id_komponen']);
			$jum_tagihan=$val_sekali['nominal'];
			$sisa=$jum_tagihan-$bayar;

			$arr_tagihan[]=array('komponen'=>$val_sekali['nama_komp'],'nominal'=>$sisa);
			if($no_sekali==$jml_sekali){
				$str_sekali.=$val_sekali['nama_komp']." Kurang : Rp. ".number_format($sisa,0,",",".")." ";
			}else{
				$str_sekali.=$val_sekali['nama_komp']." Kurang : Rp. ".number_format($sisa,0,",",".").", ";
			}
			
			$no_sekali++;
			//buat tabel
			$str_tagihan_sekali.="<tr>";
			$str_tagihan_sekali.="<td width='175'>".$val_sekali['nama_komp']."</td>";
			$str_tagihan_sekali.="<td width='10'>:</td>";
			$str_tagihan_sekali.="<td width='15'>Rp.</td>";
			//$str_tagihan_sekali.="<td align='right' width='100'>".number_format($val_sekali['nominal'],0,",",".")."</td>";
			$str_tagihan_sekali.="<td align='right' width='100'>".number_format($sisa,0,",",".")."</td>";
			$str_tagihan_sekali.="</tr>";
		}
		$tot_tunggakan=get_tunggakan_total($tapel_aktif['id_mr_th_ajaran'],$val['nis']);
		if($tot_tunggakan >0){
			$str_tagihan_sekali.="<tr>";
			$str_tagihan_sekali.="<td width='175'>Tunggakan</td>";
			$str_tagihan_sekali.="<td width='10'>:</td>";
			$str_tagihan_sekali.="<td width='15'>Rp.</td>";
			$str_tagihan_sekali.="<td align='right' width='100'>".number_format($tot_tunggakan,0,",",".")."</td>";
			
			$str_tagihan_sekali.="</tr>";
		}
		$str_tagihan_sekali.="</table>";
	}else{
		$str_tagihan_sekali="";
	}
	//echo $str_tagihan_sekali;
	$str_tunggakan="Tunggakan : ";
	
	if($tot_tunggakan >0){
		$arr_tagihan[]=array('komponen'=>'Tunggakan','nominal'=>$tot_tunggakan);
	}

	$str_tunggakan.=number_format($tot_tunggakan,0,",",".");
	$str_sms.=$str_sekali." ".$str_tahunan." ".$str_bulanan." ".$str_tunggakan;
	if(!empty($arr_tagihan)){
		$str_email="Tagihan Pembayaran Orang Tua Saudara ".$val['nm_lengkap']." untuk periode ".konvert_periode($periode_aktif['periode'])." <br>";
		$str_email .="Berikut adalah Daftar Pembayaran yang sudah Anda lakukan : ";
		$tabel_bayar="<table style='font-family: Arial;font-size: 11px;border-collapse: collapse;'>";
		$tabel_bayar.="<tr><th>No</th><th>Nama Komponen</th><th>Nominal</th></tr>";
		$no=1;
		$total_bayar=0;
		foreach($arr_tagihan as $val_bayar){
			$tabel_bayar.="<tr>";
			$tabel_bayar.="<td>".$no."</td>";
			$tabel_bayar.="<td width='175'>".$val_bayar['komponen']."</td>";
			$tabel_bayar.="<td align='right' width='100'>".number_format($val_bayar['nominal'],0,",",".")."</td>";
			$tabel_bayar.="</tr>";
			$total_bayar+=$val_bayar['nominal'];
			$no++;
		}
		$tabel_bayar.="<tr><td colspan='2'>Total</td><td><b>".number_format($total_bayar,0,",",".")."</b></td></tr>";
		$tabel_bayar.="</table>";
		$str_email.=$tabel_bayar."<br /><br />";
		$str_email.="Yayasan Salman Al-Farisi";
		
		//email tagihan baru
		$body = file_get_contents('../template_surat/surat_tagihan.html');
		$body=  str_replace('{nama_wali}', $detail_wali['nama'],$body);
		$body=  str_replace('{nama_lengkap}', $val['nm_lengkap'],$body);
		$body=  str_replace('{tagihan_bulanan}', $str_tagihan_bulanan,$body);
		$body=  str_replace('{tagihan_tahunan}', $str_tagihan_tahunan,$body);
		$body=  str_replace('{tagihan_sekali}', $str_tagihan_sekali,$body);
		$body=  str_replace('{tgl_sekarang}', tgl_indonesia(date('Y-m-d')),$body);
		$body=  str_replace('{periode_sekarang}', ambil_bulan(date('m')).' '.date('Y'),$body);
		$body=  str_replace('{nva}', $val['nis'],$body);
		$body=  str_replace('{sekolah}', $detail_kelas['nama_lembaga'],$body);
		$body=  str_replace('{kelas}', $detail_kelas['kelas'],$body);
	}
	$alamat_email="$email_siswa";
	if(($alamat_email != "") && (!empty($arr_tagihan))){
		kirim_email($alamat_email,$body);
		//echo $body;
	}
}
echo "<meta http-equiv='refresh' content='0; url=pesan.php?t=email'>";
?>